package com.hk.dao;

import com.hk.entity.Admin;
import com.hk.entity.Course;
import com.hk.utils.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class CourseDao {
    public void add(Course course) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="insert into course(cName,tId) values (?,?)";
        queryRunner.update(sql,course.getcName(),course.gettId());
    }
    public void delete(Integer cId) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="delete from course where cId=?";
        queryRunner.update(sql,cId);
    }
    public void update(Course course) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="update course set cName=?,tId=?where cId=?";
        queryRunner.update(sql,course.getcName(),course.gettId(),course.getcId());

    }
    public List<Course> list(Course course) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="select * from course";
        List<Course> list=queryRunner.query(sql,new BeanListHandler<>(Course.class));
        return list;
    }
    public Course findById(Integer cId) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="select * from course where cId=?";
        Course course=queryRunner.query(sql,new BeanHandler<>(Course.class),cId);
        return course;
    }
}
